package com.qianfeng.xinyihezi.xinyihezi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.qianfeng.xinyihezi.xinyihezi.R;
import com.qianfeng.xinyihezi.xinyihezi.adapter.XinYiBeanAdapter;
import com.qianfeng.xinyihezi.xinyihezi.config.MyNetConfig;
import com.qianfeng.xinyihezi.xinyihezi.config.MyRetrofit;
import com.qianfeng.xinyihezi.xinyihezi.model.XinYiBean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class XinYiActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener, RadioGroup.OnCheckedChangeListener {

    private String mUrl;
    private List<XinYiBean.DataBean.GoodsBean> mXinYiBean;
    private ImageView iv1;
    private TextView title,title2,content,count,back;
    private XinYiBeanAdapter adapter;
    private RecyclerView mRecyclerView;
    private RadioGroup mRadioGroup;
    private RadioButton[] mRadioButtons;
    private Retrofit mRetrofit;
    private MyRetrofit mMyRetrofit;
    private FloatingActionButton mFloatingActionButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xin_yi);
        Intent intent=getIntent();
        mUrl = intent.getStringExtra("url");
        initView();
        initData();
        setData();
        setListener();
    }

    private void setListener() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mRadioGroup.setOnCheckedChangeListener(this);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (dy>3 && mFloatingActionButton.getVisibility()== View.GONE) {
                    mFloatingActionButton.setVisibility(View.VISIBLE);
                }else if (dy<-3 && mFloatingActionButton.getVisibility()==View.VISIBLE){
                    mFloatingActionButton.setVisibility(View.GONE);
                }
            }
        });
        mFloatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRecyclerView.smoothScrollToPosition(0);
            }
        });
    }

    private void setData() {
        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        mRecyclerView.setAdapter(adapter);
    }

    private void initView() {
        mFloatingActionButton= (FloatingActionButton) findViewById(R.id.FloatingActionButton);
        mRadioGroup= (RadioGroup) findViewById(R.id.xinyi_activity_rg);
        mRadioButtons=new RadioButton[mRadioGroup.getChildCount()];
        for (int i = 0; i < mRadioButtons.length; i++) {
            RadioButton radioButton= (RadioButton) mRadioGroup.getChildAt(i);
            mRadioButtons[i]=radioButton;
        }

        back= (TextView) findViewById(R.id.back);
        mRecyclerView= (RecyclerView) findViewById(R.id.xinyi_activity_RecyclerView);
        iv1= (ImageView) findViewById(R.id.xinyi_activity_iv);
        title= (TextView) findViewById(R.id.zhutiguan_title);
        title2= (TextView) findViewById(R.id.xinyi_activity_title);
        content= (TextView) findViewById(R.id.xinyi_activity_content);
        count= (TextView) findViewById(R.id.xinyi_activity_count);
        mXinYiBean=new ArrayList<>();
        adapter=new XinYiBeanAdapter(this,mXinYiBean);
        mRecyclerView.setAdapter(adapter);
    }

    private void initData() {
        mRetrofit = new Retrofit.Builder().baseUrl(MyNetConfig.BASE_PATH).build();
        mMyRetrofit = mRetrofit.create(MyRetrofit.class);
        mMyRetrofit.getXinYiData(mUrl).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                if (response.isSuccessful()) {
                    try {
                        mXinYiBean.clear();
                            XinYiBean bean=new Gson().fromJson(response.body().string(),XinYiBean.class);
                        mXinYiBean.addAll(bean.data.goods);
                        setPic(bean);
                        adapter.notifyDataSetChanged();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }



            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });

    }
    private void setPic(XinYiBean bean) {
        Glide.with(this).load(bean.data.banner.get(0).image_url).into(iv1);
        title.setText(bean.data.activity.sub_title);
        title2.setText(bean.data.banner.get(0).name);
        content.setText(bean.data.activity.description);
        count.setText("共"+bean.data.goods_count+"件商品");
        count.setTextSize(10);
    }

    private void parseData(String type,String id){
        mMyRetrofit.getRbData(type,id).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    mXinYiBean.clear();
                    XinYiBean bean=new Gson().fromJson(response.body().string(),XinYiBean.class);
                    mXinYiBean.addAll(bean.data.goods);
                    adapter.notifyDataSetChanged();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        for (int i = 0; i < group.getChildCount(); i++) {
            if (checkedId==mRadioButtons[0].getId()) {
                initData();
            }else if (checkedId==mRadioButtons[1].getId()){
                parseData(mUrl,1+"");
            }else if (checkedId==mRadioButtons[2].getId()){
                parseData(mUrl,3+"");
            }else if(checkedId==mRadioButtons[3].getId()){
                parseData(mUrl,4+"");
            }
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    }
}
